package com.nforce.linremote;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;

public class ProgramListActivity extends FragmentActivity implements
	ProgramListFragment.Callbacks {

    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_program_list);
	getActionBar().setDisplayHomeAsUpEnabled(true);

	if (findViewById(R.id.program_detail_container) != null) {
	    mTwoPane = true;

	    ((ProgramListFragment) getSupportFragmentManager()
		    .findFragmentById(R.id.program_list))
		    .setActivateOnItemClick(true);
	}
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case android.R.id.home:
	    NavUtils.navigateUpFromSameTask(this);
	    return true;
	}
	return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
	super.onSaveInstanceState(outState);
    }

    @Override
    public void onItemSelected(Bundle arg) {
	if (mTwoPane) {
	    ProgramDetailFragment fragment = new ProgramDetailFragment();
	    fragment.setArguments(arg);
	    getSupportFragmentManager().beginTransaction()
		    .replace(R.id.program_detail_container, fragment).commit();
	} else {
	    Intent detailIntent = new Intent(this, ProgramDetailActivity.class);
	    detailIntent.putExtras(arg);
	    startActivity(detailIntent);
	}
    }
}
